package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import entity.User;

public class EasyBuyDao extends BaseDao{
	public Connection conn;
	public PreparedStatement pstmt;
	public ResultSet  rs;
  public int userName(String name){
	  int count =0;
	  try {
		conn=super.getConnection();
		String sql="SELECT  COUNT(*) from  easybuy_user WHERE loginName=? ";
		pstmt=conn.prepareStatement(sql);
		pstmt.setString(1, name);
		rs=pstmt.executeQuery();
		if(rs.next()){
			count=rs.getInt("COUNT(*)");
		}
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}finally {
		super.closeAll(conn, pstmt, rs);
	}
	return count;  
  }
  public User userDengle(String name, String pass){
	  User user=null;
	  try {
		conn=super.getConnection();
		String sql="SELECT * from  easybuy_user WHERE  loginName=? and password=?";
		pstmt=conn.prepareStatement(sql);
		pstmt.setString(1, name);
		pstmt.setString(2, pass);
		rs=pstmt.executeQuery();
		if(rs.next()){
			user = new User();
			user.setId(rs.getInt("id"));
			user.setEmail(rs.getString("email"));
			user.setIdentityCode(rs.getString("identityCode"));
			user.setUserName(rs.getString("userName"));
			user.setLoginName(rs.getString("loginName"));
			user.setMobile(rs.getString("mobile"));
			user.setPassword(rs.getString("password"));
			user.setSex(rs.getInt("sex"));
			user.setType(rs.getInt("type"));
		}
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}finally {
		super.closeAll(conn, pstmt, rs);
	}
	return user; 
  }
  public int userAdd(User user){
	  int count =0;
	  try {
		conn=super.getConnection();
		String sql="insert  into `easybuy_user`(`loginName`,`userName`,`password`,`sex`,`identityCode`,`email`,`mobile`,`type`) values (?,?,?,?,?,?,?,?)";
		pstmt=conn.prepareStatement(sql);
		pstmt.setString(1, user.getLoginName());
		pstmt.setString(2, user.getUserName());
		pstmt.setString(3, user.getPassword());
		pstmt.setInt(4, user.getSex());
		pstmt.setString(5, user.getIdentityCode());
		pstmt.setString(6, user.getEmail());
		pstmt.setString(7, user.getMobile());
		pstmt.setInt(8, user.getType());
		count=pstmt.executeUpdate();
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}finally {
		super.closeAll(conn, pstmt, rs);
	}
	return count; 
  }
}
